package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.StarStudio.Service.SoilService;
import com.ruoyi.StarStudio.Service.WarningService;
import com.ruoyi.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.StarStudio.Utils.WarningCompareUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/soils")
public class SoilController {

    @Autowired
    private SoilService soilService;

    @Autowired
    private WarningService warningService;

    //获取全部土壤信息
    @GetMapping
    public Result getAll(){
        List<Soil> soilList = null;
        try {
            soilList = soilService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_QUERY_SUCCESS, soilList);
    }

    /**
     * 导出土壤数据到 Excel表格
     * @param response HTTP响应
     * @param soil 土壤对象
     */
    @Log(title = "土壤信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Soil soil) {
        List<Soil> list = soilService.getList(soil);
        ExcelUtil<Soil> util = new ExcelUtil<Soil>(Soil.class);
        util.exportExcel(response, list, "土壤数据");
    }

    //带搜索功能分页获取土壤信息
    @GetMapping("/page")
    public PageResult getList(Soil soil){
        QueryPageUtils.startPage();
        List<Soil> soilList = null;
        PageInfo pageInfo;
        try {
            soilList = soilService.getList(soil);
            pageInfo = new PageInfo<>(soilList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.SOIL_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.SOIL_QUERY_SUCCESS, soilList);
    }

    //根据土壤id获取该条土壤信息
    @GetMapping("/byId/{soil_id}")
    public Result getBySoilId(@PathVariable long soil_id){
        Soil GetBySoilId = null;
        try {
            GetBySoilId = soilService.getBySoilId(soil_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_QUERY_SUCCESS, GetBySoilId);
    }

    //根据地块id查询土壤信息
    @GetMapping("/{zone_id}")
    public Result getAllByZoneId(@PathVariable Integer zone_id){
        List<Soil> soilList = null;
        try {
            soilList = soilService.getAllByZoneId(zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_QUERY_SUCCESS, soilList);
    }

    //分页获取某个地块的所有土壤数据
    @GetMapping("/page/{zone_id}")
    public PageResult getPageByZoneId(@PathVariable Integer zone_id){
        QueryPageUtils.startPage();
        List<Soil> soilList = null;
        PageInfo pageInfo;
        try {
            soilList = soilService.getAllByZoneId(zone_id);
            pageInfo = new PageInfo<>(soilList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.SOIL_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.SOIL_QUERY_SUCCESS, soilList);
    }

    //根据地块id查询最新一条土壤信息
    @GetMapping("/one/{zone_id}")
    public Result getNewOneByZoneId(@PathVariable Integer zone_id){
        Soil newOneByZoneId;
        Warning warningByZoneId;
        try {
            newOneByZoneId = soilService.getNewOneByZoneId(zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_QUERY_FAIL);
        }

        try {
            warningByZoneId = warningService.getByZoneId(zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.WARNING_QUERY_FAIL);
        }

        String ComRes = WarningCompareUtils.SoilCompare(newOneByZoneId, warningByZoneId);
        if (ComRes.length() > 0){
            return new Result(HttpStatusCode.WARNING, ComRes, newOneByZoneId);
        } else {
            return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_QUERY_SUCCESS, newOneByZoneId);
        }
    }

    //根据地块id增加土壤信息及中间表数据
    @PostMapping("/{zone_id}")
    public Result add(@RequestBody Soil soil, @PathVariable Integer zone_id) {
        try {
            soil.setZone_id(zone_id);
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            soil.setSoil_updateTime(timestamp);
            soilService.add(soil, zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_ADD_SUCCESS);
    }

    //修改土壤信息
    @PutMapping
    public Result update(@RequestBody Soil soil){
        try {
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            soil.setSoil_updateTime(timestamp);
            soilService.update(soil);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_UPDATE_SUCCESS);
    }

    //根据土壤id删除土壤信息
    @DeleteMapping("/{soil_id}")
    public Result delete(@PathVariable long soil_id){
        try {
            soilService.delete(soil_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.SOIL_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.SOIL_DELETE_SUCCESS);
    }
}
